<?php
/**
 * Created by PhpStorm.
 * User: Raytine
 * Date: 2019/11/21
 * Time: 21:58
 */

namespace App\Repository\Agent;


use App\Interfaces\agent\AgentInterFace;
use App\Interfaces\MapInterFace;
use App\model\agent\Agent;
use App\model\UserWorkshop;
use App\model\Workshop;
use App\model\WorkshopFabu;
use App\Service\BaiduMapService;
use App\Service\MiniProgramService;
use Illuminate\Database\Query\Builder;
use Illuminate\Http\Request;
use phpDocumentor\Reflection\DocBlock\Tag;
use Geohash\GeoHash;

class AgentRepository implements AgentInterFace
{
    protected $baiduMapService;
    protected $miniProgramService;
    public function __construct(MapInterFace $baiduMapService,MiniProgramService $miniProgramService)
    {
        $this->baiduMapService = $baiduMapService;
        $this->miniProgramService=$miniProgramService;
    }

    public function agent($agent_id): Agent
    {
        if ($agent_id == 0) {
            return Agent::whereIsDefault(1)->first();
        }
        return Agent::findOrFail($agent_id);
    }

    public function workshopList($limit)
    {
        $workshop = Workshop::with('hangye', 'qu', 'industry', 'tags')
            ->orderBy('is_zhiding','desc')
            ->orderByDesc('is_recommend', 'desc')
            ->orderByDesc('updated_at', 'desc')
            ->take($limit)
            ->get();
        return $workshop;
    }

    public function agentGeo(Agent $agent)
    {
        list($lat, $lng) = $this->baiduMapService->geocoding($agent->address);
        $agent->lat = $lat;
        $agent->lng = $lng;
        $agent->save();
    }

    /**
     * @param Request $request
     * @return \Illuminate\Contracts\Pagination\LengthAwarePaginator
     */
    public function workshopSearch(Request $request)
    {

        $sort_key = $request->get('sort_key', 'updated_at');

        $sort=$request->get('sort','desc');
        $province_id = $request->get('province_id', '');
        $city_id = $request->get('city_id', '');
        $qu_id = $request->get('qu_id', '');
        $tags = $request->get('tags', '');
        $hangye = $request->get('hangye', '');
        $industry = $request->get('industry', '');
        $purpose = $request->get('purpose', '');
        $area=$request->get('area','');
        $status=$request->get('status');
        $floor=$request->get('floor');
        $searchKey=$request->get('search_key');
        $workshop = Workshop::with('hangye', 'qu', 'industry', 'tags');
        $workshop->orderBy('is_zhiding','desc');

        if ($sort_key) {
            $workshop = $workshop->orderBy($sort_key=='id' ? 'updated_at' : $sort_key ,$sort);
        };
        if ($province_id) {
            $workshop = $workshop->where('p_province_id', $province_id);
        }
        if ($city_id) {
            $workshop = $workshop->where('p_city_id', $city_id);
        }
        if ($qu_id) {
            $workshop = $workshop->where('p_district_id', $qu_id);
        }
        if ($floor){
            $workshop=$workshop->where('floor', $floor);
        }
        if ($searchKey){
            $workshop=$workshop->where(function ($query)use ($searchKey){
                $query->where('title','like','%'.$searchKey.'%')
                    ->orWhere('address','like','%'.$searchKey.'%');
            });
        }
        //标签
        if ($tags) {
            $workshop = $workshop->whereHas('tags', function ($builder) use ($tags) {
                $tags = explode(',', $tags);
                $builder->whereIn('workshop_tag.id', $tags);
            });
        }
        //行业
        if ($hangye) {
            $workshop = $workshop->whereHas('hangye', function ($builder) use ($hangye) {
                $hangye = explode(',', $hangye);
                $builder->whereIn('workshop_hangye.id', $hangye);
            });
        }
        //类型
        if ($industry) {
            $workshop = $workshop->whereHas('industry', function ($builder) use ($industry) {
                $industry = explode(',', $industry);
                $builder->whereIn('workshop_industry.id', $industry);
            });
        }
        //用途
        if ($purpose) {
            $workshop = $workshop->whereHas('purpose', function ($builder) use ($purpose) {
                $purpose = explode(',', $purpose);
                $builder->whereIn('workshop_purpose.id', $purpose);
            });
        }
        //面积
        if ($area){
            switch ($area) {
                case 1:
                    $workshop = $workshop->where('area', '<', 1000);
                    break;
                case 2:
                    $workshop = $workshop->where('area', '>=', 1000)
                        ->where('area', '<=', 3000);
                    break;
                case  3:
                    $workshop = $workshop->where('area', '>=', 3000)
                        ->where('area', '<=', 6000);
                    break;
                case  4:
                    $workshop = $workshop->where('area', '>=', 6000)
                        ->where('area', '<=', 10000);
                    break;
                case  5:
                    $workshop = $workshop->where('area', '>=', 10000);
                    break;
            }
        }
        //状态
        if ($status!==""&&$status!==null){
            $workshop=$workshop->where('status',$status);
        }
        return $workshop->paginate(10);
    }

    public function workshop($id)
    {
        $workshop = Workshop::with('hangye', 'industry', 'tags', 'qu', 'zhen','park')->where('id', $id)->first();
        return $workshop;
    }
    public function getQrcode(Agent $agent)
    {
        $file_name=$this->miniProgramService->miniCode($agent->id,'pages/index/index');
        $agent->qrcode=$file_name;
        $agent->save();
    }
    public function workshopNearList($request)
    {
        $latitude = $request->get('latitude', '');
        $longitude = $request->get('longitude', '');
        $search_key=$request->get('search_key');
        $area = $request->get('area', '5');
      /*  $GeoHash = new GeoHash();
        $geo = $GeoHash->encode($latitude, $longitude);*/
        $workshop = Workshop::with('hangye', 'qu', 'industry', 'tags');

        if ($search_key){
            $workshop=$workshop->where(function ($query)use ($search_key){
                $query->where('title','like','%'.$search_key.'%')
                    ->orWhere('address','like','%'.$search_key.'%');
            });
        }
        //距离
        if ($area){
            switch ($area) {
                case 1:
                   // $workshop = $workshop->where('geo', 'like', substr($geo,0,6).'%');
                    $workshop = $workshop->whereRaw("(acos(sin((?*3.1415)/180) * sin((latitude*3.1415)/180)
                    +cos((?*3.1415)/180) * cos((latitude*3.1415)/180) * cos((?*3.1415)/180
                     - (longitude*3.1415)/180))*6370.996)<=2",[$latitude,$latitude,$longitude]);
                    break;
                case 2:
                   // $workshop = $workshop->where('geo', 'like', substr($geo,0,5).'%');
                    $workshop = $workshop->whereRaw("(acos(sin((?*3.1415)/180) * sin((latitude*3.1415)/180)
                    +cos((?*3.1415)/180) * cos((latitude*3.1415)/180) * cos((?*3.1415)/180
                     - (longitude*3.1415)/180))*6370.996)>2",[$latitude,$latitude,$longitude])->whereRaw("(acos(sin((?*3.1415)/180) * sin((latitude*3.1415)/180)
                    +cos((?*3.1415)/180) * cos((latitude*3.1415)/180) * cos((?*3.1415)/180
                     - (longitude*3.1415)/180))*6370.996)<=5",[$latitude,$latitude,$longitude]);
                    break;
                case 3:
                    $workshop = $workshop->whereRaw("(acos(sin((?*3.1415)/180) * sin((latitude*3.1415)/180)
                    +cos((?*3.1415)/180) * cos((latitude*3.1415)/180) * cos((?*3.1415)/180
                     - (longitude*3.1415)/180))*6370.996)>5",[$latitude,$latitude,$longitude])->whereRaw("(acos(sin((?*3.1415)/180) * sin((latitude*3.1415)/180)
                    +cos((?*3.1415)/180) * cos((latitude*3.1415)/180) * cos((?*3.1415)/180
                     - (longitude*3.1415)/180))*6370.996)<=10",[$latitude,$latitude,$longitude]);
                    break;
                case 4:
                   // $workshop = $workshop->where('geo', 'like', substr($geo,0,3).'%');
                    break;
                case 5:
                    $workshop = $workshop->whereRaw("(acos(sin((?*3.1415)/180) * sin((latitude*3.1415)/180)
                    +cos((?*3.1415)/180) * cos((latitude*3.1415)/180) * cos((?*3.1415)/180
                     - (longitude*3.1415)/180))*6370.996)>10",[$latitude,$latitude,$longitude]);
                    break;
                default:
                    break;
            }
        }
        $workshop = $workshop
            ->orderBy('is_zhiding','desc')
            ->orderByDesc('is_recommend', 'desc')
            ->orderByDesc('id', 'desc');
        return $workshop->paginate(10);
    }

    public function workshopUserFabu($request)
    {
        $shenhe = $request->get('status', '');
        $workshop = WorkshopFabu::with('hangye', 'qu', 'industry', 'tags');
        if($shenhe){
            $workshop = $workshop->where('shenhe',$shenhe);
        }
        $workshop = $workshop
            ->orderBy('created_at','desc');
        return $workshop->paginate(10);
    }

    public function workshopUserStart($request)
    {
        $user_id = 100;
        $workshopIds= UserWorkshop::getWorkshopIds($user_id);
        $workshop = Workshop::with('hangye', 'qu', 'industry', 'tags');
        $workshop = $workshop
            ->whereIn('id',$workshopIds)
            ->orderBy('created_at','desc')
            ->paginate(10);
        return $workshop;
    }
}
